package com.agriculture.competition.controller;

import com.agriculture.common.core.utils.poi.ExcelUtil;
import com.agriculture.common.core.web.controller.BaseController;
import com.agriculture.common.core.web.domain.AjaxResult;
import com.agriculture.common.core.web.page.TableDataInfo;
import com.agriculture.common.log.annotation.Log;
import com.agriculture.common.log.enums.BusinessType;
import com.agriculture.common.security.annotation.RequiresPermissions;
import com.agriculture.competition.domain.JaFile;
import com.agriculture.competition.service.IJaFileService;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件上传Controller
 *
 * @author agriculture
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/file")
public class JaFileController extends BaseController {

    @Autowired
    private IJaFileService jaFileService;

    /**
     * 查询文件上传列表
     */
    @RequiresPermissions("competition:file:list")
    @GetMapping("/list")
    public TableDataInfo list(JaFile jaFile) {
        startPage();
        List<JaFile> list = jaFileService.selectJaFileList(jaFile);
        return getDataTable(list);
    }

    /**
     * 导出文件上传列表
     */
    @RequiresPermissions("competition:file:export")
    @Log(title = "文件上传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JaFile jaFile) {
        List<JaFile> list = jaFileService.selectJaFileList(jaFile);
        ExcelUtil<JaFile> util = new ExcelUtil<JaFile>(JaFile.class);
        util.exportExcel(response, list, "文件上传数据");
    }

    /**
     * 获取文件上传详细信息
     */
    @RequiresPermissions("competition:file:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jaFileService.selectJaFileById(id));
    }

    /**
     * 新增文件上传
     */
    @RequiresPermissions("competition:file:add")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JaFile jaFile) {
        return toAjax(jaFileService.insertJaFile(jaFile));
    }

    /**
     * 修改文件上传
     */
    @RequiresPermissions("competition:file:edit")
    @Log(title = "文件上传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JaFile jaFile) {
        return toAjax(jaFileService.updateJaFile(jaFile));
    }

    /**
     * 删除文件上传
     */
    @RequiresPermissions("competition:file:remove")
    @Log(title = "文件上传", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jaFileService.deleteJaFileByIds(ids));
    }

    /**
     * 根据外键查询文件信息
     *
     * @param foreignKey
     * @return
     */
    @GetMapping(value = "/queryByForeignKey")
    public AjaxResult queryByForeignKey(@RequestParam("foreignKey") Long foreignKey) {
        return success(jaFileService.queryByForeignKey(foreignKey));
    }
}
